package com.naire.modul.survey_org.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.cusorg.pojo.Cusorg;
import com.naire.modul.project.pojo.Project;
import com.naire.modul.survey_org.dao.Survey_orgDao;
import com.naire.modul.survey_org.pojo.Survey_org;
import com.naire.modul.survey_org.vo.Survey_orgVo;

public class Survey_orgDaoImpl extends GeneralDaoImpl implements Survey_orgDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Survey_orgVo> search(PageBean pageBean, Integer project_id)
			throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("project_id", project_id);
		StringBuilder hql = new StringBuilder(
				"from Survey_org where project.project_id=:project_id");
		List<Survey_orgVo> d = new ArrayList<Survey_orgVo>();
		List<Survey_org> l = hibernateHandler.query(hql, pageBean, params);
		for (Survey_org bean : l) {
			Survey_orgVo dv = new Survey_orgVo();
			BeanUtils.copyProperties(bean, dv);
			Cusorg o = bean.getCusorg();
			BeanUtils.copyProperties(o, dv);
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(Survey_orgVo bean) throws Exception {
		Project project = (Project) hibernateHandler.find(Project.class, bean.getProject_id());
		String[] orgIds = bean.getOrg_ids().split(",");
		if(orgIds != null) {
			int i = 0;
			for(String orgId : orgIds) {
				Survey_org pojo = findByProjectAndOrg(bean.getProject_id(), Integer.parseInt(orgId));
				if(pojo == null) {
					pojo = new Survey_org();
					Cusorg cusorg = (Cusorg)hibernateHandler.find(Cusorg.class, Integer.parseInt(orgId));
					pojo.setProject(project);
					pojo.setProject_num(project.getProject_num());
					pojo.setCusorg(cusorg);
					pojo.setOrg_num(cusorg.getOrg_num());
					hibernateHandler.insert(pojo);
					if(i % 20 == 0) {
						hibernateHandler.flush();
					}
					i++;
				}
			}
		}
	}

	@Override
	public void update(Survey_orgVo bean) throws Exception {
		Survey_org pojo = (Survey_org) hibernateHandler.find(Survey_org.class,
				bean.getSurvey_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(Survey_orgVo bean) throws Exception {
		update(bean);
	}

	@Override
	public Survey_orgVo find(Integer id) throws Exception {
		Survey_org pojo = (Survey_org) hibernateHandler.find(Survey_org.class,
				id);
		Survey_orgVo bean = new Survey_orgVo();
		BeanUtils.copyProperties(pojo, bean);
		return bean;
	}
	
	@SuppressWarnings("unchecked")
	private Survey_org findByProjectAndOrg(Integer project_id, Integer org_id) throws Exception {
		StringBuilder hql = new StringBuilder("from Survey_org where project.project_id=:project_id and cusorg.org_id=:org_id");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("project_id", project_id);
		params.put("org_id", org_id);
		List<Survey_org> list = hibernateHandler.query(hql, params);
		if(list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void delete(Integer id) throws Exception {
		Survey_org pojo = (Survey_org) hibernateHandler.find(Survey_org.class,
				id);
		hibernateHandler.delete(pojo);
	}
}
